<?php

class Database
{
 
 public static function escape($data)
 {
  mysql_connect(Config::get('host'), Config::get('user'), Config::get('password'));
  return mysql_real_escape_string($data);
 }
 
 public static function query($query)
 {
  mysql_connect(Config::get('host'), Config::get('user'), Config::get('password'));
  mysql_select_db(Config::get('database')) or die ("Connection error");
  $result = mysql_query($query);

  $to_return = array();
  
  $rows = 0;
  while ($row = mysql_fetch_assoc($result))
  {
   $to_return[] = $row;
   $rows++;
  }
  
  if (!$result) 
  {
  	echo $query;
  	echo mysql_error();
  }
  return $to_return;
 }
 
 public static function insert($query)
 {
  mysql_connect(Config::get('host'), Config::get('user'), Config::get('password'));
  mysql_select_db(Config::get('database')) or die ("Connection error");
  $result = mysql_query($query);
  
  if (!$result) 
  {
  	echo $query;
  	echo mysql_error();
  }
  if ($result) return mysql_insert_id();
  
  else return false;
 }
 
 public static function update($query)
 {
  mysql_connect(Config::get('host'), Config::get('user'), Config::get('password'));
  mysql_select_db(Config::get('database')) or die ("Connection error");
  $result = mysql_query($query);
  
  if (!$result) 
  {
  	echo $query;
  	echo mysql_error();
  }
  if ($result) return mysql_insert_id();
  
  else return false;
 }
 
 public static function save($table, $data)
 {

     $fields = array_keys($data);
     $values = array_values($data);
     
     //Update
     if ($data['id']) 
     {
         $updates = array();
         var_dump($data);
         foreach ($data as $field => $val)
         {
             if ($field != 'id') $updates[] = "`$field` = '".Database::escape($val)."'";
         }
         
         $update_str = implode(',', $updates);
         
         $query = "UPDATE `$table` SET $update_str WHERE `id` = $data[id]";
         echo $query;
         return Database::insert($query);
     }
     //Insert
     else
     {
         
         foreach ($values as $i=>&$val)
         {
             $values[$i] = "'".Database::escape($val)."'";
         }
      
         $field_str = implode(',', $fields);
         $value_str = implode(',', $values);
         
         $query = "INSERT INTO `$table` ($field_str) VALUES ($value_str)";
         return Database::insert($query);
         
     }
 }
 
 
 function find($table, $conditions, $fields = '*', $order = '', $limit = '100000')
 {
     if ($order) $order_str = "ORDER BY $order";
     else $order_str = '';
     
     if (!$fields) $fields = '*';
     
     if ($conditions == null)
     {
         $condition_str = "1";
     }
     else
     {
         $condition_parts = array();
         foreach ($conditions as $field=>$val)
         {
             $condition_parts[] = "`$field` = '".Database::escape($val)."'";
         }
         
         $condition_str = implode(' AND ', $condition_parts);
     }
    
     $query = "SELECT $fields FROM `$table` WHERE $condition_str $order_str LIMIT $limit";

     return Database::query($query);     
 }
} 
?>
